package com.yanqu.road.server.cmd.zhaoshang;

import com.yanqu.road.entity.zhaoshang.ZsProjectNotice;
import com.yanqu.road.entity.zhaoshang.ZsProjectNoticeParam;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.zhaoshang.ZhaoShangProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.zhaoshang.ZsMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;

@Cmd(code = CrossProtocol.C2_ZHAO_SHANG_PROJECT_NOTICE_LIST, desc = "项目公告")
public class ZsProjectNoticeListCmd  extends CrossCommand {

    private long projectId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ZhaoShangProto.CrossZsProjectNoticeReqMsg reqMsg = ZhaoShangProto.CrossZsProjectNoticeReqMsg.parseFrom(packet.getBytes());
        projectId = reqMsg.getProjectId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ZhaoShangProto.ZsProjectNoticeRespMsg.Builder respMsg = ZhaoShangProto.ZsProjectNoticeRespMsg.newBuilder();
        List<ZsProjectNotice> zsProjectNoticeList = ZsMgr.getZsProjectNotice(projectId);
        if(zsProjectNoticeList != null){
            for(ZsProjectNotice notice : zsProjectNoticeList){
                ZhaoShangProto.ZsProjectNoticeInfoTemp.Builder build = ZhaoShangProto.ZsProjectNoticeInfoTemp.newBuilder();
                build.setProjectId(notice.getProjectId());
                build.setAddTime(notice.getAddTime());
                build.setType(notice.getNoticeType());
                for(ZsProjectNoticeParam param : notice.getParamList()){
                    ZhaoShangProto.ZsProjectNoticeParamTemp.Builder paramBuilder = ZhaoShangProto.ZsProjectNoticeParamTemp.newBuilder();
                    paramBuilder.setServerId(param.getServerId());
                    paramBuilder.setUserId(param.getUserId());
                    paramBuilder.setTitleId(param.getTitleId());
                    paramBuilder.setCity(param.getCity());
                    paramBuilder.setNickName(param.getNickName());
                    build.addParams(paramBuilder);
                }
                respMsg.addNoticeList(build);
            }
        }
        respMsg.setProjectId(projectId);
        respMsg.setRet(0);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_ZS_PROJECT_NOTICE_LIST, respMsg);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), pbMsg);
    }

    @Override
    public void sendData() {

    }
}
